
Shader "ShaderMan/SnackShader"
	{

	Properties{
        _Alpha ("Alpha", Float) = 1.0
        _Pow ("Pow", Float) = 1.0
	//Properties
	}

	SubShader
	{
	Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }

	Pass
	{
	ZWrite Off
	Blend SrcAlpha OneMinusSrcAlpha

	CGPROGRAM
	#pragma vertex vert
	#pragma fragment frag
	#include "UnityCG.cginc"
    
    fixed _Alpha;
    fixed _Pow;

	struct VertexInput {
    fixed4 vertex : POSITION;
	fixed2 uv:TEXCOORD0;
    fixed4 tangent : TANGENT;
    fixed3 normal : NORMAL;
	//VertexInput
	};


	struct VertexOutput {
	fixed4 pos : SV_POSITION;
	fixed2 uv:TEXCOORD0;
	//VertexOutput
	};

	//Variables

	#define pi 3.1415926535

fixed2x2 Rot(fixed a) {
    fixed s=sin(a), c=cos(a);
    return fixed2x2(c, -s, s, c);
}
fixed2 N(fixed angle) {
    return fixed2(sin(angle), cos(angle));
}

fixed circle(fixed2 p, fixed r){
   return length(p)-r;
}

fixed4 smin(fixed4 a, fixed4 b, fixed k) {
    fixed h = clamp(0.5 + 0.5*(a.a-b.a)/max(k,0.01), 0.0, 1.0);
    return lerp(a, b, h) - k*h*(1.0-h);
}

VertexOutput vert (VertexInput v)
{
    VertexOutput o;
    o.pos = UnityObjectToClipPos (v.vertex);
    o.uv = v.uv;
    //VertexFactory
    return o;
}
fixed4 frag(VertexOutput i) : SV_Target
{

    fixed t =  (_Pow *_Time.y*0.05+100.);
    fixed2 uv = (i.uv - 1 * 0.5) / 1;
    uv*=2.;
    
    fixed2 id = floor(uv);
    fixed dir = 0.;
    // fixed2x2 rot = Rot(dir*pi);
    // uv*=rot;
    fixed2 center = fixed2(0.,0.);
    
    fixed4 d1 = fixed4(0,0,0,10000);
    fixed4 d2 = fixed4(0,0,0,10000);
    fixed n = 45.;
    fixed r = 0.04;//point size
    fixed dr = 0.08;//point scale
    fixed dk = 3.;//max k
    fixed w = 1.;//1 - circle, 2 - eight
    fixed sk = 3.;//speed of the worm
    
    fixed f = pi*2/n;
    fixed z = t*16;
    fixed s = 3.;//speed of circle rotation
    fixed i1 = -z*s+floor(z*0.5)*(2.*sk+2.*s);
    fixed i2 = i1+n;;
    z = z*f*sk+pi-f*s;//+dir*f*n/4.;
    [unroll(100)]
    for(fixed i=i1;i<i2;i++)
    {
    fixed a = i*f;

    fixed2 co = fixed2(cos(a), sin(a*w)*1.);//position of single circle
    co.x = sign(co.x)*pow(abs(co.x), w)*w;

    fixed c = circle(uv-co*0.7-center,r+dr*(cos((a)*2.)*.5+.5));
    
    fixed ka = (a+z*(dir*2.-1.))*1.;
    fixed k = cos(ka)*.5+.5;
    k = pow(k, 250.)*dk;
    
    fixed tr = fmod(i-i1, 3.);
    fixed3 col = fixed3(tr!=0., tr!=1.,tr!=2.);
    fixed4 cc = fixed4(col, c);
    
    k*= 9./n;
    if (i-i1<n/3.) d1 = smin(d1, cc, k);
    else  d2 = smin(d2, cc, k);
    }
    fixed4 dd = smin(d1, d2,0.)*5;

    fixed3 col = fixed3(0,0,0);
    col+=dd.rgb*smoothstep(.3,.0, dd.a);

    float y = z+0.07;
    float c = circle(uv-center,0.02);

    float d = dd.a*.5;
    col-=fixed3(5,5,5)*smoothstep(.01,.0, c);
    return fixed4(col,_Alpha);
}
ENDCG
  }
  
}
    }